package com;

import com.alibaba.fastjson.JSON;
import io.netty.util.concurrent.CompleteFuture;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2024/4/1-2024
 * @Description: 这里用一句话描述这个类的作用
 */
public class Test {
    public static void main0(String[] args) throws IOException {
        /*RandomAccessFile raf = new RandomAccessFile("E:\\rocketMq 笔记\\笔记\\abc1.txt", "rw");
        raf.setLength(1024L * 1024); // 设置文件大小为1GB
        raf.close();*/

        try (RandomAccessFile raf = new RandomAccessFile("E:\\rocketMq 笔记\\笔记\\a.txt", "rw");) {
            // 设置文件大小为1MB（注意这里是1024 * 1024，不是1GB）
            raf.setLength(1024L * 1024);
            //FileChannel channel = raf.getChannel();
            //channel.map()

            // 将文件指针定位到文件的开头
            raf.seek(0);

            // 写入字符串"hello world"
            String content = "hello world";
            raf.write(content.getBytes());

            // 可选：如果需要将文件指针再次移动到文件开头以准备读取或其他操作
            // raf.seek(0);
        } catch (IOException e) {
            e.printStackTrace();
        }


        CompletableFuture<Void> voidCompletableFuture =
                CompletableFuture.runAsync(new Runnable() {
                            @Override
                            public void run() {

                            }
                        }, Executors.newFixedThreadPool(5))
                        .whenComplete(new BiConsumer<Void, Throwable>() {
                            @Override
                            public void accept(Void unused, Throwable throwable) {

                            }
                        })
                        .exceptionally(new Function<Throwable, Void>() {
                            @Override
                            public Void apply(Throwable throwable) {
                                return null;
                            }
                        })
                        .handle(new BiFunction<Void, Throwable, Void>() {
                            @Override
                            public Void apply(Void unused, Throwable throwable) {
                                return null;
                            }
                        })
                ;
    }

    public static void main(String[] args) throws Exception {
        //设置 Property 属性
        System.setProperty("name","yanxu");
        System.out.println(System.getProperties());

        //System.exit(-1);       //非0代表非正常退出程序。
        //System.exit(0);  //0代表正常退出程序。
        System.out.println("main方法执行!!!");
        ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        /*scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("1 initDelay");
            }
        },1,10, TimeUnit.SECONDS);*/
        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("1 initDelay");
            }
        },10,10, TimeUnit.SECONDS);


        System.out.println(System.getProperty("user.home"));

        System.out.println(Runtime.getRuntime().availableProcessors());

        //JSON.toJSONString("aaa",true);
        System.out.println(Math.max(10000, Math.min(1000 * 30, 60000)));

        //钩子函数。程序结束的时候执行
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("哈哈哈");
            }
        }));
        System.out.println("1111");
        //System.exit(-1);

        FileChannel channel = new FileOutputStream("").getChannel();
        //MappedByteBuffer map = channel.map();

    }
}